package cn.tedu.system.oa.stamp.service.impl;

import cn.tedu.system.oa.stamp.dao.repository.StampCacheRepository;
import cn.tedu.system.oa.stamp.mapper.StampMapper;
import cn.tedu.system.oa.stamp.pojo.dto.StampQuery;
import cn.tedu.system.oa.stamp.pojo.entity.Stamp;
import cn.tedu.system.oa.stamp.pojo.param.StampAddParam;
import cn.tedu.system.oa.stamp.service.StampService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class StampServiceImpl implements StampService {

    @Autowired
    private StampMapper stampMapper;
    @Autowired
    private StampCacheRepository stampCacheRepository;
    @Override
    public void saveStamp(StampAddParam stampAddParam) {
        log.debug("新建印章业务:stampAddParam={}", stampAddParam);

        if (stampAddParam != null) {
            Stamp stamp = new Stamp();
            BeanUtils.copyProperties(stampAddParam,stamp);
            if (stampAddParam.getId() == null) {
            //新建印章功能
                stampAddParam.setCreateTime(new Date());
                stampMapper.insert(stamp);
            } else {
//                更新印章功能
                stamp.setUpdateTime(new Date());
                UpdateWrapper updateWrapper = new UpdateWrapper();
                updateWrapper.eq("id", stampAddParam.getId());
                stampMapper.update(stamp, updateWrapper);
            }
            //变动后重置存入redis缓存
            List<Stamp> list = stampMapper.selectList(null);
            stampCacheRepository.saveAllStamp(list);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("删除印章业务:id={}",id);

        stampMapper.deleteById(id);

        //变动后重置存入redis缓存
        List<Stamp> list = stampMapper.selectList(null);
        stampCacheRepository.saveAllStamp(list);

    }


    @Override
    public List<Stamp> selectList(StampQuery stampQuery) {
        log.debug("查询印章业务:stampQuery={}",stampQuery);

        List<Stamp> list = null;
        QueryWrapper<Stamp> queryWrapper = new QueryWrapper<>();
        if (stampQuery != null) {
            log.info("有查询条件:{}", stampQuery);
            if (stampQuery.getManageId() != null) {
                queryWrapper.eq("manage_id", stampQuery.getManageId());
            }
            if (stampQuery.getName() != null) {
                queryWrapper.like("name", stampQuery.getName());
            }
            if (stampQuery.getStatus() != null) {
                queryWrapper.eq("status", stampQuery.getStatus());
            }
            if (stampQuery.getType() != null) {
                queryWrapper.eq("type", stampQuery.getType());
            }
            if (stampQuery.getCreateTime() != null) {
                long time = stampQuery.getCreateTime().getTime();
                queryWrapper.like("create_time", time);
            }
           if(stampQuery.getManageId() == null && stampQuery.getName() == null
                   && stampQuery.getStatus() == null && stampQuery.getType() == null && stampQuery.getCreateTime() == null){
               log.debug("无查询条件则用Redis查全表:{}",stampQuery);
               List<Stamp> allStamp = stampCacheRepository.getAllStamp();
               if (!CollectionUtils.isEmpty(allStamp)) {
                   log.info("Redis查全表:{}", allStamp);
                   return allStamp;
               }else {
                   log.debug("Redis缓存无，数据库查全表:{}", allStamp);
                   list = stampMapper.selectList(null);
                   log.debug("数据库表存入Redis");
                   stampCacheRepository.saveAllStamp(list);
                   log.debug("数据库表存入Redis成功");
                   return list;
               }
           }
            queryWrapper.orderByDesc("create_time");
            list = stampMapper.selectList(queryWrapper);
        }
        return list;
    }

}
